package com.hermit.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hermit.system.dao.ClassInfoDao;
import com.hermit.system.dao.CollegeInfoDao;
import com.hermit.system.dao.MajorInfoDao;
import com.hermit.system.dao.UserStudentDao;
import com.hermit.system.dto.CollegeMajorClassStudentDTO;
import com.hermit.system.entity.ClassInfoEntity;
import com.hermit.system.entity.CollegeInfoEntity;
import com.hermit.system.entity.MajorInfoEntity;
import com.hermit.system.entity.UserStudentEntity;
import com.hermit.system.service.CollegeInfoService;
import com.hermit.system.service.MajorInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("collegeInfoService")
public class CollegeInfoServiceImpl extends ServiceImpl<CollegeInfoDao, CollegeInfoEntity> implements CollegeInfoService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MajorInfoDao majorInfoDao;
    @Autowired
    private ClassInfoDao classInfoDao;
    @Autowired
    private UserStudentDao userStudentDao;

    /**
     * 查询数据库所有表
     */
    public List<CollegeInfoEntity> queryAll() {
        List<CollegeInfoEntity> allDataList = baseMapper.queryAll();
        System.out.println("查询结果:" + allDataList);
        return allDataList;
    }

    /**
     * 删除
     */
    public void delete(int id) {
        CollegeInfoEntity collegeInfoEntity = baseMapper.selectById(id);
        collegeInfoEntity.setDeleteFlag(1);
        baseMapper.updateById(collegeInfoEntity);
    }

    /**
     * 查询学院对应数量
     */
    public List<CollegeInfoEntity> queryCount() {
        List<CollegeInfoEntity> collegeInfoEntities = baseMapper.queryAll();
        for (CollegeInfoEntity collegeInfoEntity : collegeInfoEntities) {
            // 查询学院对应专业数目
            QueryWrapper<MajorInfoEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("college_id", collegeInfoEntity.getId());
            queryWrapper.eq("delete_flag", 0);
            List<MajorInfoEntity> majorInfoEntities = majorInfoDao.query(queryWrapper);
            collegeInfoEntity.setMajorInfoEntityList(majorInfoEntities);
            // 查询学院对应班级数目
            QueryWrapper<ClassInfoEntity> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("co.id", collegeInfoEntity.getId());
            queryWrapper.eq("c.delete_flag", 0);
            List<ClassInfoEntity> classInfoEntities = classInfoDao.query(queryWrapper1);
            collegeInfoEntity.setClassInfoEntityList(classInfoEntities);
            // 查询学院对应学生数目
            QueryWrapper<UserStudentEntity> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("co.id", collegeInfoEntity.getId());
            queryWrapper2.eq("s.delete_flag", 0);
            List<UserStudentEntity> userStudentEntities = userStudentDao.query(queryWrapper2);
            collegeInfoEntity.setUserStudentEntityList(userStudentEntities);
        }
        return collegeInfoEntities;
    }
}